package com.itheima.service.mongo;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private MongoTemplate mongoTemplate;


    //保存喜欢信息
    @Override
    public void save(Long userId, Long likeUserId) {

        // 查询我是否已经喜欢了对方
        Query query = new Query(
                Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId)
        );
        boolean isExists = mongoTemplate.exists(query, UserLike.class);

        //如果不存在  添加
        if (!isExists) {
            UserLike userLike = new UserLike();
            userLike.setCreated(System.currentTimeMillis());
            userLike.setUserId(userId);
            userLike.setLikeUserId(likeUserId);

            mongoTemplate.save(userLike);
        }

    }


    //查询两人是否为好友关系
    @Override
    public boolean isMutualLike(Long userId, Long likeUserId) {

        //查询我是否喜欢了对方
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId)
        );

        boolean exists = mongoTemplate.exists(query, UserLike.class);

        //查询对方是否喜欢了我
        Query query2 = new Query(Criteria.where("userId").is(likeUserId)
                .and("likeUserId").is(userId)
        );

        boolean exists2 = mongoTemplate.exists(query2, UserLike.class);

        return exists && exists2;
    }


    //删除喜欢信息
    @Override
    public void delete(Long userId, Long likeUserId) {
        //删除 我--喜欢--她
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId)), UserLike.class);
    }


    //获取三个喜欢总数
    @Override
    public UserLikeCountVo findUserCount(Long userId) {

        //获取互相喜欢的总数
        Query query = new Query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, Friend.class);

        //获取我喜欢的总数
        Query query1 = new Query(Criteria.where("userId").is(userId));
        long count1 = mongoTemplate.count(query1, UserLike.class);

        //获取喜欢我的总数
        Query query2 = new Query(Criteria.where("likeUserId").is(userId));
        long count2 = mongoTemplate.count(query2, UserLike.class);

        //封装
        UserLikeCountVo userLikeCountVo = new UserLikeCountVo();

        userLikeCountVo.setEachLoveCount(count);
        userLikeCountVo.setLoveCount(count1);
        userLikeCountVo.setFanCount(count2);

        return userLikeCountVo;
    }



    //喜欢详情
    @Override
    public PageBeanVo findUserList(Long userId, Integer type, Integer pageNum, Integer pageSize) {

        if (type == 1) {
            //互相关注
            Query query = new Query(Criteria.where("userId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);
            List<Friend> friendList = mongoTemplate.find(query, Friend.class);

            //是否为空
            List<UserLikeVo> userLikeVoList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(friendList)) {
                for (Friend friend : friendList) {
                    Long friendId = friend.getFriendId();

                    //取值封装
                    UserLikeVo userLikeVo = new UserLikeVo();
                    userLikeVo.setId(friendId);
                    userLikeVo.setMatchRate(findScore(userId, friendId).intValue());

                    userLikeVoList.add(userLikeVo);
                }
            }

            //获取总数
            long count = mongoTemplate.count(query, Friend.class);

            return new PageBeanVo(pageNum, pageSize, count, userLikeVoList);

        } else if (type == 2) {
            //我关注
            Query query = new Query(Criteria.where("userId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);
            List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

            //是否为空
            List<UserLikeVo> userLikeVoList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(userLikeList)) {
                for (UserLike userLike : userLikeList) {
                    Long LikeUserId = userLike.getLikeUserId();

                    //取值封装
                    UserLikeVo userLikeVo = new UserLikeVo();
                    userLikeVo.setId(LikeUserId);
                    userLikeVo.setMatchRate(findScore(userId, LikeUserId).intValue());

                    userLikeVoList.add(userLikeVo);
                }
            }

            //获取总数
            long count = mongoTemplate.count(query, UserLike.class);

            return new PageBeanVo(pageNum, pageSize, count, userLikeVoList);

        } else if (type == 3) {
            //粉丝
            Query query = new Query(Criteria.where("likeUserId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);
            List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

            //是否为空
            List<UserLikeVo> userLikeVoList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(userLikeList)) {
                for (UserLike userLike : userLikeList) {
                    Long likeUserId = userLike.getUserId();

                    //取值封装
                    UserLikeVo userLikeVo = new UserLikeVo();
                    userLikeVo.setId(likeUserId);
                    userLikeVo.setMatchRate(findScore(likeUserId, userId).intValue());

                    //判断是否已经喜欢了你的粉丝
                    if (isMutualLike(userId, likeUserId)) {
                        userLikeVo.setAlreadyLove(true);
                    }

                    userLikeVoList.add(userLikeVo);
                }
            }

            //获取总数
            long count = mongoTemplate.count(query, UserLike.class);

            return new PageBeanVo(pageNum, pageSize, count, userLikeVoList);

        } else {
            //谁看过我
            Query query = new Query(Criteria.where("userId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);
            List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);

            //是否为空
            List<UserLikeVo> userLikeVoList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(visitorList)) {
                for (Visitor visitor : visitorList) {
                    Long LikeUserId = visitor.getVisitorUserId();

                    //取值封装
                    UserLikeVo userLikeVo = new UserLikeVo();
                    userLikeVo.setId(LikeUserId);
                    userLikeVo.setMatchRate(findScore(userId, LikeUserId).intValue());

                    userLikeVoList.add(userLikeVo);
                }
            }

            //获取总数
            long count = mongoTemplate.count(query, Visitor.class);

            return new PageBeanVo(pageNum, pageSize, count, userLikeVoList);
        }

    }


    //查询匹配值
    public Double findScore(Long userId, Long friendId) {
        //查询条件
        Query query = new Query(Criteria.where("userId").is(friendId).and("toUserId").is(userId));

        //查询
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        //判断是否为空
        if (recommendUser == null) {
            //赋默认值
            return RandomUtil.randomDouble(80, 99);
        }

        return recommendUser.getScore();
    }

}
